﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bll;
using Model;
using System.Data;
using System.Net;
using System.IO;
using System.Text;

namespace Accounting.admin.Notice
{
    public partial class NoticeAdd : System.Web.UI.Page
    {
        ModelPublic m = new ModelPublic();
        BllPublic b = new BllPublic();

        protected void Page_Load(object sender, EventArgs e)
        {

            if (Session["Name"] != null && Session["Type"] != null)
            {
                txtAuthor.Text = Session["Name"].ToString();

                lblUserType.Text = Session["Type"].ToString();
                txtDateTime.Text = DateTime.Now.ToString("yyyy-MM-dd");



                if (lblUserType.Text == "0")
                {
                    Release.Visible = true;
                }
                else
                {
                    Release.Visible = false;
                }

            }
            else
            {
                ASubmit.Disabled = false;
                btnRest.Enabled = false;
            }
        }

        protected void Submit_Click(object sender, EventArgs e)
        {
            string folderRouteFile = "";

            m.Number ="'"+ txtNumber.Text.Trim() + "'";
            m.DateTime = "'" + txtDateTime.Text.Trim() + "'";
            m.Author = "'" + txtAuthor.Text.Trim() + "'";
            m.Content =Content.Text;
            m.Title = "'" + txtTitle.Text.Trim() + "'";
            if (Important.Checked == true)
                m.Important = "'True'";
            else
                m.Important = "'False'";
            if (lblUserType.Text == "0")
            {
                m.Remarks = "NULL";
                if (Release.Checked == true)
                    m.Release = "'True'";
                else
                    m.Release = "'False'";
            }
            else
            {
                m.Remarks = "'stu'";
                m.Release = "'False'";
            }



            if (ReleaseFile.HasFile)
            {
                string Upname = ReleaseFile.FileName;
                int Pointposition = 0;
                string FileName = "";
                string suffix = "";

                for (int i = 0; i < Upname.Length; i++)
                {
                    if (Upname[i] == '.')
                    {
                        Pointposition = i;
                        break;
                    }
                }

                for (int i = 0; i < Pointposition; i++)
                {
                    FileName += Upname[i];
                }

                for (int i = Pointposition; i < Upname.Length; i++)
                {
                    suffix += Upname[i];
                }

                m.DateTime = txtDateTime.Text.Trim();
                m.NavId = "12";

                int num = b.Notice_Week_Count_File_Number(m);

                string Time = System.DateTime.Now.ToString("yyyyMMdd");
                folderRouteFile = "InformationFile/" + Time +12+ (num + 1).ToString() + suffix;

                m.FileRoute = "'" + folderRouteFile + "'";
                m.FileName = "'" + ReleaseFile.FileName + "'";
            }
            else
            {
                m.FileName = "NULL";
                m.FileRoute = "NULL";
            }

            string message = b.Notice_Week_Add(m);
            if (message == "1")
            {
                Response.Write("<script>alert('添加成功!')</script>");

                if(ReleaseFile.HasFile)
                {
                    try
                    {
                        ReleaseFile.SaveAs(Server.MapPath("../../" + folderRouteFile));
                    }
                    catch (Exception ex)
                    {
                        Response.Write("<script>alert('上传失败:" + ex.Message + "')</script> ");
                    }
                }

                m.Title = txtTitle.Text.Trim();
                m.NavId = "12";
                string Id = b.Notice_Week_Id(m);
                AddNews(Id);
            }
            else
            {
                Response.Write("<script>alert('记录添加失败或标题已经存在！')</script>");
            }

        }
        protected void btnRest_Click(object sender, EventArgs e)
        {
            ClearContext();
        }

        private void ClearContext()
        {
            txtNumber.Text = "0";
            txtDateTime.Text = "";
            txtTitle.Text = "";
            Content.Text = "";
            txtAuthor.Text = "";
        }
        private void AddNews(string id)
        {
            string stid = Request["stid"];//信息公开网站相应的类别
            string stracc = "ckuivf";
            string strpwd = "fcsaezxcbn3d";


            string istb = Request.Params["istb"];//
            if (!string.IsNullOrEmpty(istb))
            {
                Adddate(id, stid, txtTitle.Text.Trim(), null, txtDateTime.Text.Trim(), Content.Text, stracc, strpwd, "http://xxgk.wzvtc.cn/data/adddata");
            }
        }

        public string Adddate(string id, string stid, string title, string ctitle, string postdate, string Article, string unames, string upass, string url)
        {


            string requestBody = string.Format("iid={0}&tid={1}&title={2}&TitleColor={3}&postdate={4}&Article={5}&unames={6}&upass={7}"

               , HttpUtility.UrlEncode(id, Encoding.GetEncoding("utf-8"))
               , HttpUtility.UrlEncode(stid, Encoding.GetEncoding("utf-8"))
               , HttpUtility.UrlEncode(title, Encoding.GetEncoding("utf-8"))
               , HttpUtility.UrlEncode(ctitle, Encoding.GetEncoding("utf-8"))
               , HttpUtility.UrlEncode(postdate, Encoding.GetEncoding("utf-8"))
               , HttpUtility.UrlEncode(Article, Encoding.GetEncoding("utf-8"))
                , HttpUtility.UrlEncode(unames, Encoding.GetEncoding("utf-8"))
                  , HttpUtility.UrlEncode(upass, Encoding.GetEncoding("utf-8"))
               , string.Empty
                );
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.Method = "POST";
            request.KeepAlive = false;
            request.ContentType = "application/x-www-form-urlencoded";
            byte[] aryBuf = Encoding.GetEncoding("utf-8").GetBytes(requestBody);
            request.ContentLength = aryBuf.Length;
            using (Stream writer = request.GetRequestStream())
            {
                writer.Write(aryBuf, 0, aryBuf.Length);
                writer.Close();
                writer.Dispose();
            }
            string ret = string.Empty;
            using (WebResponse response = request.GetResponse())
            {
                StreamReader reader = new StreamReader(response.GetResponseStream()
                    , Encoding.GetEncoding("utf-8")
                    );
                ret = reader.ReadToEnd();
                reader.Close();
                reader.Dispose();
            }

            return ret;
        }
    }
}